package dao.impl;

import dao.EmployeeDao;
import pojo.Department;
import pojo.Employee;
import pojo.Manage;
import view.EmployeeView;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmployeeImpl implements EmployeeDao {
    private List<Employee> employeeList = Manage.getEmployeeList();

    @Override
    public void addEmployee(Employee employee)  {
        employeeList.add(employee);
        Manage.saveEmployee(employeeList);
    }

    @Override
    public void delEmployee(int id) {
        boolean exist = false;
        Iterator iterator = employeeList.iterator();
        while(iterator.hasNext()){
            Employee employee = (Employee) iterator.next();
            if(employee.getEmployeeId() == id){
                exist = true;
                employeeList.remove(employee);
                break;
            }
        }
        if(exist)
            Manage.saveEmployee(employeeList);
        else
            System.out.println("该员工id不存在");
    }

    @Override
    public Employee selectEmployee(int id){
        Iterator iterator = employeeList.iterator();
        while(iterator.hasNext()){
            Employee employee = (Employee) iterator.next();
            if(employee.getEmployeeId() == id){
                return employee;
            }
        }
        return null;
    }

    @Override
    public List selectEmployeeByDep(int id) {
        List<Employee> list = new ArrayList<>();
        Iterator iterator = employeeList.iterator();
        while(iterator.hasNext()){
            Employee employee = (Employee) iterator.next();
            if(employee.getDepartment().getId() == id){
                list.add(employee);
            }
        }
        return list;
    }

    @Override
    public List selectEmployeeBySalary(String str) {
        List<Employee> list = new ArrayList();
        Matcher match = Pattern.compile("[<>=]+").matcher(str);
        Matcher matchSalary = Pattern.compile("\\d+.\\d+").matcher(str);
        double money = 0;
        String s = null;
        while(match.find()){
            s = match.group();
        }
        while(matchSalary.find()) {
            money = Double.parseDouble(matchSalary.group());
        }
        if(s.equals(">")){
            for(Employee employee: employeeList){
                if(employee.getSalary() > money) list.add(employee);
            }
        }
        if(s.equals("<")){
            for(Employee employee: employeeList){
                if(employee.getSalary() < money) list.add(employee);
            }
        }
        if(s.equals(">=")){
            for(Employee employee: employeeList){
                if(employee.getSalary() >= money) list.add(employee);
            }
        }
        if(s.equals("<=")){
            for(Employee employee: employeeList){
                if(employee.getSalary() <= money) list.add(employee);
            }
        }
        if(s.equals("=")){
            for(Employee employee: employeeList){
                if(employee.getSalary() == money) list.add(employee);
            }
        }
        return list;
    }

    @Override
    public void updateEmployee(int id,String name,String sex,int age,double salary,Department department) {
        int updateId = 0;
        Iterator iterator = employeeList.iterator();
        while(iterator.hasNext()){
            Employee employee = (Employee) iterator.next();
            if(employee.getEmployeeId() == id){
                employee.update(name,sex,age,salary,department);
                employeeList.set(updateId,employee);
                break;
            }
            updateId += 1;
        }
        Manage.saveEmployee(employeeList);
    }

    @Override
    public void showAllEmployee(){
        EmployeeView.printAllEmployee(employeeList);
    }
}
